package com.bjsxt.config;

import java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 雷哥
 * @Date: 2020/10/11/17:10
 * @Description:
 */
@Configuration
public class DealyMessageConfig {

  /**
   * 声明一个延时队列
   */
  @Bean
  public Queue dealyQueue(){
    Map<String, Object> args=new HashMap<>();
    // 把一个队列修改为延迟队列
    args.put("x-message-ttl",10*1000) ; // 消息的最大存活时间
    args.put("x-dead-letter-exchange","DeadLetter.exc") ; // 该队列里面的消息死了，去那个交换机
    args.put("x-dead-letter-routing-key","DeadLetter.key") ; // 该队列里面的消息死了，去那个交换机, 由那个路由key 路由他
    Queue queue=new Queue("dealy",true,false,false,args);
    return queue;
  }

  /**
   * 声明死信交换机
   */
  @Bean
  public DirectExchange deadLetterExchange(){
    return new DirectExchange("DeadLetter.exc");
  }

  /**
   * 声明一个新的队列
   */
  @Bean
  public Queue newQueue(){
    return new Queue("new.queue");
  }

  /**
   * 把死信交换机和新的队列绑定
   */
  @Bean
  public Binding newAndDeadLetterExchange(){
    return BindingBuilder.bind(newQueue()).to(deadLetterExchange())
        .with("DeadLetter.key"); //死信路由key
  }

}
